package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Logs;
import com.fc.campusapplicationsystem.model.Unit;
import com.fc.campusapplicationsystem.service.LogsService;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 贺哲
 * @2020-03-02 22:24
 */
@RestController
@RequestMapping("/logsController")
public class LogsController {

    @Autowired
    LogsService logsService;

    @GetMapping("/list")
    public ResponseOBJ<List<Logs>> list(@RequestParam("page") Integer page,
                                        @RequestParam("limit") Integer limit) {
        ResponseOBJ<List<Logs>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<Logs> assets = logsService.list();
            PageInfo<Logs> pageInfo = new PageInfo<>(assets);
            responseOBJ.setData(assets);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody Logs logs) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = logsService.add(logs);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }
}
